//
//  SDView.m
//  TJJDownLoadDemo
//
//  Created by WangJiaheng on 16/1/20.
//  Copyright © 2016年 Shixiongwei. All rights reserved.
//

#import "SDView.h"
#import "SDBaseProgressView.h"

@implementation SDView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)]];//添加点击事件
    }
    return self;
}

- (void)setProgressViewClass:(Class)progressViewClass
{
    _progressViewClass = progressViewClass;
    self.progressView = [[progressViewClass alloc] init];
    [self addSubview:self.progressView];
}

#pragma mark -
#pragma mark 点击事件
- (void)tap
{
    if (self.label == nil)
    {//只创建1次
        self.label = [[UILabel alloc] init];
        NSLog(@"只创建1次");
    }
    
    if (self.progressView.progress*100 >= 99.999000)
    {//如果进度条大于 则执行点击按钮效果   点击之后  开始升级
        if ([NSStringFromClass(self.progressViewClass) isEqualToString:@"SDBallProgressView"])
        {//下载的进度条
            UserConfig * config = [UserConfig sharingUserConfig];
            [config setUpdateHardWare:YES]; //修改标记
            self.progressView.progress = -1;
            //发送通知 改变进度的样式
            NSNotification *notification =[NSNotification notificationWithName:@"removeLabelOfAlertDown" object:nil userInfo:nil];
            [[NSNotificationCenter defaultCenter] postNotification:notification];
        }
        else if ([NSStringFromClass(self.progressViewClass) isEqualToString:@"SDPieLoopProgressView"])
        {//固件升级的进度条
            NSLog(@"固件升级的进度条");
        }
        else
        {//其他的
        }
    }
    else
    {//什么也不做，也就是现在不可点击的效果。
        if ([NSStringFromClass(self.progressViewClass) isEqualToString:@"SDPieLoopProgressView"])
        {//固件升级的进度条
        }
    }
}


+ (id)ViewWithClass:(Class)class
{
    SDView *view = [[self alloc] init];
    view.progressViewClass = class;
//    [view setBackgroundColor:[UIColor redColor]];//测试使用
    return view;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.progressView.frame = self.bounds;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
